﻿Imports System
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Threading

Namespace Controlers


    Public Class LinkItem(Of TView As {UIElement, ILinksView(Of TView), IHeatView(Of TView), ITeamSystemView(Of TView)})
        Inherits Items.Item
        Implements Items.IItemWrapper(Of WorkItem)
        Implements Items.IItemWrapper(Of Controlers.Data.Entities.CallLog)



#Region " Values "

        Private m_WorkItem As WorkItem
        Private m_WorkItemStatus As WorkItemStatus
        Private m_Call As Controlers.Data.Entities.CallLog
        Private m_Controler As LinksControler(Of TView)

#End Region

#Region " Propertys "

        Public ReadOnly Property Controler() As LinksControler(Of TView)
            Get
                Return m_Controler
            End Get
        End Property

        Public Property WorkItem() As WorkItem Implements Items.IItemWrapper(Of WorkItem).Value
            Get
                Return m_WorkItem
            End Get
            Set(ByVal value As WorkItem)
                If m_WorkItem Is Nothing OrElse Not m_WorkItem.Rev = value.Rev Then
                    m_WorkItem = value
                    OnPropertyChanged("WorkItem")
                    OnPropertyChanged("HasWorkItem")
                End If
            End Set
        End Property

        Public ReadOnly Property HasWorkItem() As Boolean
            Get
                Return Not WorkItem Is Nothing
            End Get
        End Property

        Public Property WorkItemStatus() As WorkItemStatus
            Get
                Return m_WorkItemStatus
            End Get
            Set(ByVal value As WorkItemStatus)
                If Not m_WorkItemStatus = value Then
                    m_WorkItemStatus = value
                    OnPropertyChanged("WorkItemStatus")
                End If
            End Set
        End Property

        Public Property CallLog() As Controlers.Data.Entities.CallLog Implements Items.IItemWrapper(Of Controlers.Data.Entities.CallLog).Value
            Get
                Return m_Call
            End Get
            Set(ByVal value As Controlers.Data.Entities.CallLog)
                If Not m_Call Is value Then
                    m_Call = value
                    OnPropertyChanged("CallLog")
                    OnPropertyChanged("CallLogHasTFSId")
                End If
            End Set
        End Property

        Public ReadOnly Property CallLogHasTFSId() As Boolean
            Get
                Return Controler.View.HeatControler.GetCallLogsTFSRef(Me.CallLog.RefNo) > 0
            End Get
        End Property

#End Region

        Public Sub New(ByVal linksControler As LinksControler(Of TView), ByVal CallLog As Controlers.Data.Entities.CallLog)
            MyBase.New(CallLog.CallID)
            m_Controler = linksControler
            Me.m_Call = CallLog
            m_WorkItemStatus = WorkItemStatus.NotLoaded
            LoadWorkItem()
        End Sub

        Public Sub LoadWorkItem()
            Dim HeatTFSRef As Integer = Controler.View.HeatControler.GetCallLogsTFSRef(Me.CallLog.RefNo)
            If HeatTFSRef > 0 Then
                System.Threading.ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf LoadWorkItemFromCall))
            Else
                System.Threading.ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf LoadWorkItemFromTFS))
            End If
        End Sub

        Public Sub LoadWorkItemFromCall()
            Dim TFSRef As Integer = Controler.View.HeatControler.GetCallLogsTFSRef(Me.CallLog.RefNo)
            If TFSRef > 0 Then
                Try
                    Me.WorkItem = Controler.View.TeamSystemControler.WorkItemStore.GetWorkItem(TFSRef)
                    WorkItemStatus = WorkItemStatus.Loaded
                Catch ex As Exception
                    WorkItemStatus = WorkItemStatus.Failed
                End Try
            Else
                WorkItemStatus = WorkItemStatus.NotLoaded
            End If
        End Sub

        Public Sub LoadWorkItemFromTFS()
            Dim HeatID As Integer = CInt(Me.m_Call.CallID)
            Dim wit As WorkItem = Controler.View.TeamSystemControler.GetWorkItemFromHeatCallLogID(HeatID)
            If Not wit Is Nothing Then
                Me.WorkItem = wit
                WorkItemStatus = WorkItemStatus.Loaded
            Else
                WorkItemStatus = WorkItemStatus.NotLoaded
            End If
        End Sub

    End Class

    Public Enum WorkItemStatus
        NotLoaded
        Loaded
        Failed
    End Enum


End Namespace